package com.dingshi.security.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingshi.security.R;
import com.dingshi.security.ServicesReceiverPaperActivity;
import com.dingshi.security.entity.Track;
import com.dingshi.security.util.Utils;

public class ServicesAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private Context context;
	private ArrayList<Track> list;

	public ServicesAdapter(Context context, ArrayList<Track> list) {
		this.context = context;
		this.list = list;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Track track = list.get(position);
		if (track == null) {
			return null;
		}
		final ViewHolder holder;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.services_item, null);
			holder = new ViewHolder();
			holder.trackItem = (LinearLayout) convertView
					.findViewById(R.id.services_item);
			holder.devicename = (TextView) convertView
					.findViewById(R.id.devicename);
			holder.time = (TextView) convertView.findViewById(R.id.timestamp);
			holder.device_name = (TextView) convertView.findViewById(R.id.device_name);
			holder.status = (TextView) convertView.findViewById(R.id.status);
			holder.btn_look = (Button) convertView.findViewById(R.id.btn_look);
			holder.trackItem.setId(position);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		updateData(holder, track);

		OnClickListener clickListener = new OnClickListener() {

			@Override
			public void onClick(final View v) {
				switch (v.getId()) {
				case R.id.btn_look:
					Intent	intent = new Intent(context,ServicesReceiverPaperActivity.class);
					intent.putExtra("oddNum", list.get(((LinearLayout) v.getParent().getParent()).getId()).getOddNum());
					context.startActivity(intent);

					break;
				default:
					break;
				}
			}
		};
		holder.btn_look.setOnClickListener(clickListener);
		return convertView;
	}
	
	

	private void updateData(ViewHolder holder, Track track) {

		holder.devicename.setText(track.getArea());
		String start_time = track.getReportDate();
		if(!Utils.isNullOrEmpty(start_time)) {
			String[] split = start_time.split(" ");
			holder.time.setText(split[0]);
		}else {
			holder.time.setText(track.getReportDate());
		}
		holder.device_name.setText(track.getDeviceName());
		holder.btn_look.setText("维修");
		
		if ("3".equals(track.getStatus())) {
			holder.btn_look.setVisibility(View.VISIBLE);
			holder.status.setText("维修中");
		} else if ("4".equals(track.getStatus())) {
			holder.status.setText("维修完毕");
			holder.btn_look.setVisibility(View.GONE);
		}
	}

	static class ViewHolder {
		public TextView device_name;
		public LinearLayout trackItem;
		public TextView devicename;
		public TextView time;
		public TextView status;
		public Button btn_look;
	}
}
